package com.hzit.ms.service.impl;

import com.hzit.ms.mapper.SkGoodsInfoMapper;
import com.hzit.ms.model.SkillGoodsInfo;
import com.hzit.ms.resp.GoodsData;
import com.hzit.ms.resp.SkGoodsDetailData;
import com.hzit.ms.service.IGoodsService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import org.springframework.util.ObjectUtils;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.concurrent.TimeUnit;

/**
 * 商品相关接口实现类
 */
@Service
public class GoodsServiceImpl implements IGoodsService {

    @Autowired
    private ValueOperations<String,Object> valueOperations;

    @Autowired
    private SkGoodsInfoMapper skGoodsInfoMapper;


    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");




    @Override
    public List<GoodsData> getSkillGoodsList() {

        //丢到redis

        //1.先查询redis
        String skGoodList = "skGoodList";
        List<SkillGoodsInfo>  skillGoodsInfos = (List<SkillGoodsInfo> ) valueOperations.get(skGoodList);

        if (CollectionUtils.isEmpty(skillGoodsInfos)){
            skillGoodsInfos = skGoodsInfoMapper.querySkillGoods();
            valueOperations.set(skGoodList,skillGoodsInfos,2, TimeUnit.HOURS);
        }

        List<GoodsData> goodsDataList = new ArrayList<GoodsData>();

        for (SkillGoodsInfo skillGoodsInfo :skillGoodsInfos){

            GoodsData goodsData = new GoodsData();

            BeanUtils.copyProperties(skillGoodsInfo,goodsData);

            goodsDataList.add(goodsData);

        }

        return goodsDataList;
    }

    @Override
    public SkGoodsDetailData getSkillGoodsDetail(String goodsId) {

        String redisKey = "sk:goodsDetail:" + goodsId;
        SkillGoodsInfo skillGoodsInfo = (SkillGoodsInfo)valueOperations.get(redisKey);
        if(ObjectUtils.isEmpty(skillGoodsInfo)){
            skillGoodsInfo = skGoodsInfoMapper.querySkillGoodsDetail(goodsId);
            valueOperations.set(redisKey,skillGoodsInfo,2, TimeUnit.HOURS);

        }
        SkGoodsDetailData skGoodsDetailData = new SkGoodsDetailData();
        //bean复制，两个bean里面的字段要一样
        BeanUtils.copyProperties(skillGoodsInfo,skGoodsDetailData);

        Date startDate = skillGoodsInfo.getStartDate();
        Date endDate =  skillGoodsInfo.getEndDate();

        skGoodsDetailData.setStartDate(sdf.format(startDate));
        skGoodsDetailData.setEndDate(sdf.format(endDate));


        Date nowDate = new Date();

        //  0 - 活动进行中个， 1- 活动未开始， -1 - 活动已结束

        if (nowDate.after(startDate) && nowDate.before(endDate)){
            skGoodsDetailData.setRemainSeconds("0");
        }

        if (nowDate.before(startDate)){
            skGoodsDetailData.setRemainSeconds("1");
        }

        if (nowDate.before(startDate)){
            skGoodsDetailData.setRemainSeconds("1");
        }

        return skGoodsDetailData;
    }
}
